<?php
/**
 * WB_S3Response_ObjectList class
 * 
 * @author Jean-Lou Dupont
 * @version @@package-version@@
 * @id $Id: S3Response_ObjectList.php 144 2008-03-14 18:57:42Z jldupont $
 */

 //{{include
 require_once 'web-bloks/AmazonS3/responses/S3Response.php';
 require_once 'web-bloks/AmazonS3/info/S3ObjectInfo.php';
 //include}}
 
 class WB_S3Response_ObjectList
 	extends WB_S3Response
 	implements Iterator
 {
 	/**
 	 * If the bucket contains more
 	 * objects than what this response holds
 	 */
 	var $isTruncated = null;
	
	/**
	 * For iteration purpose
	 */
	var $nextMarker = null;
	
 	/**
 	 * Constructor
 	 */
	public function __construct( &$rawResponse )
	{
		parent::__construct( $rawResponse );
	}
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%		
	// INTERNAL methods
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%				
	protected function init()
	{
		$this->extractTruncated();
		
		$this->extractNextMarker();
		
		$objects = $this->extractObjects();
		
		if ( is_null ( $objects ))
			return;
		
		foreach( $objects as $index =>$raw )
		{
			$objectInfo = WB_ObjFactory::factory( 'WB_S3ObjectInfo' );
			$objectInfo->initFromRaw( $raw );
			
			$this->liste[ $index ] = $objectInfo ;
		}
		return $this;
	}
	protected function extractTruncated()
	{
		if ( empty( $this->raw ) )
			return null;
			
		$r = preg_match("@<"."IsTruncated>(.*?)<"."/IsTruncated>@", $this->raw, $match);
		if (($r === false) || ($r === 0))
			return;
			
		$this->isTruncated = ( strtolower( $match[1] ) == "true" );
		
		return $this;		
	}
	protected function extractNextMarker()
	{
		if ( empty( $this->raw ) )
			return null;
			
		$r = preg_match("@<"."NextMarker>(.*?)<"."/NextMarker>@", $this->raw, $match);
		if (($r === false) || ($r === 0))
			return;
			
		$this->nextMarker = $match[1];
		
		return $this;		
	}
	protected function extractObjects()
	{
		if ( empty( $this->raw ) )
			return null;
			
		$r = preg_match_all("@<"."Contents>(.*?)<"."/Contents>@", $this->raw, $matches);
		if (($r === false) || ($r === 0))
			return null;
			
		return $matches[1];			
	}
	
 } // end class declaration
 
 /* SAMPLE RESPONSE (from s3-dg-20060301.pdf)
    -----------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<ListBucketResult xmlns="http://doc.s3.amazonaws.com/2006-03-01">
	<Name>quotes</Name>
	<Prefix>N</Prefix>
	<Marker>Ned</Marker>
	<MaxKeys>40</MaxKeys>
	<IsTruncated>false</IsTruncated>
	<Contents>
		<Key>Nelson</Key>
		<LastModified>2006-01-01T12:00:00.000Z</LastModified>
		<ETag>&quot;828ef3fdfa96f00ad9f27c383fc9ac7f&quot;</ETag>
		<Size>5</Size>
		<StorageClass>STANDARD</StorageClass>
		<Owner>
			<ID>bcaf1ffd86f41caff1a493dc2ad8c2c281e37522a640e161ca5fb16fd081034f</ID>
			<DisplayName>webfile</DisplayName>
		</Owner>
	</Contents>
	<Contents>
		<Key>Neo</Key>
		<LastModified>2006-01-01T12:00:00.000Z</LastModified>
		<ETag>&quot;828ef3fdfa96f00ad9f27c383fc9ac7f&quot;</ETag>
		<Size>4</Size>
		<StorageClass>STANDARD</StorageClass>
		<Owner>
			<ID>bcaf1ffd86f41caff1a493dc2ad8c2c281e37522a640e161ca5fb16fd081034f</ID>
			<DisplayName>webfile</DisplayName>
		</Owner>
	</Contents>
</ListBucketResult>
 */